<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index(){
        $cateobj = M('category');
        $caterow = $cateobj->order(array('cateparent'=>'asc'))->select();//查询数据时对parent父级排序
        $treeobj = new \Org\Util\Tree;//引用Tree类
        $row = $treeobj->getTree($caterow,$pid = 0, $col_id = 'cateid', $col_pid = 'cateparent', $col_cid = 'catehaschild');//$col_id,$col_pid,$col_cid对应分类表category中的字段
        $list = M('goods')->order('goodsid DESC')->select();
        $this->assign('list', $list);
        $this->assign('cateRow', $row);
        $this->display();
    }

    public function release(){
        $cateobj = M('category');
        $where['cateparent'] = 0;
        $caterow = $cateobj->order(array('cateparent'=>'asc'))->select();//查询数据时对parent父级排序
        $treeobj = new \Org\Util\Tree;//引用Tree类
        $row = $treeobj->getTree($caterow,$pid = 0, $col_id = 'cateid', $col_pid = 'cateparent', $col_cid = 'catehaschild');//$col_id,$col_pid,$col_cid对应分类表category中的字段
        $catelist = $cateobj->where($where)->order('cateid DESC')->select();
        $this->assign('cateRow', $row);
        $this->assign('catelist', $catelist);
    	$this->display();
    }

    public function register(){
    	$this->display();
    }

    public function register_do(){
    	$user = M('user');
    	$data['username'] = I('post.username','');
		$data['usercode'] = md5(I('post.usercode', ''));
		$data['createtime'] = time();
		if($user->add($data)) {
			$this->success('用户注册成功', '/Com/login');
		} else {
			$this->error($mod_user->getError(), '/Index/register');
		}
    }

    public function loginout() {
        session('user_info', null);//用户登录
        $this->success('登出功能','index');
    }

    public function picupload(){
        $return = array('code' => 0, 'info' => '未知错误', 'content' => array());
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 314572800 ;// 设置附件上传大小
        $upload->rootPath = './Public/img/1';// 设置附件上传根目录
        $upload->savePath = '/'; // 设置附件上传（子）目录
        $upload->saveName = array('uniqid','');
        $upload->autoSub  = false;
        $return['content'] = '';
        $reupload = $upload->upload();    
        if($reupload && $reupload === false){ // 上传错误提示错误信息
            $return['info'] = $upload->getError();
            exit(json_encode($return));
        } else {
            $modelname['photo'] = $reupload[0]['savename'];
            $return['content'] = array('picurl' => '/Public/img/1/'.$modelname['photo']);
            $return['code'] = 1;
            $return['info'] = '上传成功';
            exit(json_encode($return));
        }
        $return['code'] = 1;
        $return['info'] = '上传成功';
        exit(json_encode($return));
    }

    public function picupload1(){
        $return = array('code' => 0, 'info' => '未知错误', 'content' => array());
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 314572800 ;// 设置附件上传大小
        $upload->rootPath = './Public/img/2';// 设置附件上传根目录
        $upload->savePath = '/'; // 设置附件上传（子）目录
        $upload->saveName = array('uniqid','');
        $upload->autoSub  = false;
        $return['content'] = '';
        $reupload = $upload->upload();    
        if($reupload && $reupload === false){ // 上传错误提示错误信息
            $return['info'] = $upload->getError();
            exit(json_encode($return));
        } else {
            $modelname['photo'] = $reupload[0]['savename'];
            $return['content'] = array('picurl1' => '/Public/img/2/'.$modelname['photo']);
            $return['code'] = 1;
            $return['info'] = '上传成功';
            exit(json_encode($return));
        }
        $return['code'] = 1;
        $return['info'] = '上传成功';
        exit(json_encode($return));
    }

    public function picupload2(){
        $return = array('code' => 0, 'info' => '未知错误', 'content' => array());
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 314572800 ;// 设置附件上传大小
        $upload->rootPath = './Public/img/3';// 设置附件上传根目录
        $upload->savePath = '/'; // 设置附件上传（子）目录
        $upload->saveName = array('uniqid','');
        $upload->autoSub  = false;
        $return['content'] = '';
        $reupload = $upload->upload();    
        if($reupload && $reupload === false){ // 上传错误提示错误信息
            $return['info'] = $upload->getError();
            exit(json_encode($return));
        } else {
            $modelname['photo'] = $reupload[0]['savename'];
            $return['content'] = array('picurl2' => '/Public/img/3/'.$modelname['photo']);
            $return['code'] = 1;
            $return['info'] = '上传成功';
            exit(json_encode($return));
        }
        $return['code'] = 1;
        $return['info'] = '上传成功';
        exit(json_encode($return));
    }

    public function picupload_do(){
        $date['babyname']= I('post.babyname','');
        $date['category']= I('post.category','');
        $date['babymoney']= I('post.babymoney','');
        $date['phone']= I('post.phone','');
        $date['qq']= I('post.qq','');
        $date['clientname']= I('post.clientname','');
        $date['address']= I('post.address','');
        $date['babycont']= I('post.babycont','');
        $date['stunum']= I('post.stunum','');
        $date['picurl']= I('post.picurl','');
        $date['picurl1']= I('post.picurl1','');
        $date['picurl2']= I('post.picurl2','');
        $date['day']= time();
        $userdate['username'] = I('post.stunum','');
        $userdate['usercode'] = md5(I('post.usercode',''));
        $userdate['createtime'] = time();
        $where['username'] = I('post.stunum','');
        $user = M('user');
        $goods = M('goods');
        $finduser = $user->where($where)->find();
        if(!empty($finduser)){
            $this->error('您是我们的会员了，请在个人中心里添加宝贝');
        }else{
            if($addgoods = $goods->add($date)){
                $adduser = $user->add($userdate);
                $this->success('发布宝贝成功', '/Com/login');
            }else{
                $this->error('发布宝贝失败', '/Index/release');
            }
        }
    }

    public function getMore(){
        $last_id = $this->_get('last_id');
        $map['goodsid'] = array('lt', $last_id);
        $list = M('goods')->where($map)->order('goodsid DESC')->limit(3)->select();
        $this->ajaxReturn($list);
    }

    public function waterfall(){

            }
}